package com.lcc.book.demos.web.Controller;

import com.lcc.book.demos.web.Model.UserInfo;
import com.lcc.book.demos.web.Service.UserService;
import com.lcc.book.demos.web.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        //校验参数的合法性
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        //调用service查找数据中是否存在userName对应的用户
        UserInfo userInfo = userService.getUserInfoByUsername(userName);
        //校验用户是否存在
        if(userInfo == null || userInfo.getDeleteFlag() == 1){
            return false;
        }
        //校验密码是否正确
        if (password.equals(userInfo.getPassword())){
            //账号密码正确，保存到session
            session.setAttribute(Constants.SESSION_USER_KEY,userInfo);
            return true;
        }
        return false;
    }
}
